package com.dnake.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.dnake.Constant;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.R;
import com.dnake.utils.AppUtils;
import com.dnake.utils.ToastUtil;
import com.dnake.utils.VoipUtils;
import com.orhanobut.logger.Logger;
import com.wx.android.common.util.SharedPreferencesUtils;
import com.yuntongxun.ecsdk.ECDevice;
import com.yuntongxun.ecsdk.ECError;
import com.yuntongxun.ecsdk.ECInitParams;
import com.yuntongxun.ecsdk.SdkErrorCode;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class VoipPhoneFragment extends SupportFragment {
    @Bind(R.id.rb_app_one)
    RadioButton rbAppOne;
    @Bind(R.id.rb_app_two)
    RadioButton rbAppTwo;
    private View mView;

    public static VoipPhoneFragment newInstance() {
        return new VoipPhoneFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_reboot, container, false);
        ButterKnife.bind(this, mView);
        if (SharedPreferencesUtils.getBoolean(Constant.SP_PHONE_STATUS)) {
            rbAppOne.setChecked(true);
        } else {
            rbAppTwo.setChecked(true);
        }
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_REBOOT) {
            if ("1".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getBoolean(Constant.SP_PHONE_STATUS)) {
                    rbAppTwo.setChecked(true);
                    ECDevice.logout(new ECDevice.OnLogoutListener() {
                        @Override
                        public void onLogout() {
                            ToastUtil.show("关闭成功");
                        }
                    });
                    SharedPreferencesUtils.put(Constant.SP_PHONE_STATUS, false);
                } else {
                    rbAppOne.setChecked(true);
                    VoipUtils.login(getActivity());
                    ToastUtil.show("开启成功");
                    SharedPreferencesUtils.put(Constant.SP_PHONE_STATUS, true);
                }
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
